package com.yksj.healthtalk.ui.home;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;
import org.universalimageloader.core.display.RoundedBitmapDisplayer;
import org.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.DoctorPatientListEntity;
import com.yksj.healthtalk.entity.UrlEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.JsonParseUtils;
/**
 * 我的医生  更多
 * @author apple
 *
 */
public class MyDoctorMoreActivity extends BaseFragmentActivity implements OnItemClickListener, OnClickListener {

	private PullToRefreshListView listView;
	private MyAdapter myAdapter;
	private List<DoctorPatientListEntity> doctorList = new ArrayList<DoctorPatientListEntity>();

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.my_docotor_more_layout);
		initView();
	}
	@Override
	protected void onStart() {
		super.onStart();
		Intent intent =new Intent(this,CoreService.class);
		intent.setAction(CoreService.ACTION_FRAME_VIEW_VISIABLE);
		intent.putExtra("className",this.getClass().getName());
		startService(intent);
	}
	private void initView() {
		initTitle();
		listView = (PullToRefreshListView) findViewById(R.id.listview_fragment);
		myAdapter = new MyAdapter();
		listView.setAdapter(myAdapter);
		titleTextV.setText("更多");
		listView.setOnItemClickListener(this);
		titleLeftBtn.setOnClickListener(this);
	}
	
	private void initData() {
		HttpRestClient.doHttpMyDoctorList(SmartFoxClient.getLoginUserId(), new HttpHander());
	}

	@Override
	protected void onResume() {
		super.onResume();
		listView.setRefreshing();
		initData();
	}
	/**
	 * http请求
	 * 
	 * @author Administrator
	 * 
	 */
	class HttpHander extends AsyncHttpResponseHandler {

		@Override
		public void onSuccess(String content) {
			if (JsonParseUtils.filterErrorMessage(content) == null) {
				if (doctorList.size() != 0) doctorList.clear();
//				doctorList.addAll(JsonParseUtils.getMyDoctorListData(content));
				doctorList.addAll(DoctorPatientListEntity.parseToList(content));
				myAdapter.entities.clear();
				myAdapter.entities.addAll(doctorList);
				myAdapter.notifyDataSetChanged();
			}
			super.onSuccess(content);
		}
		@Override
		public void onFinish() {
			super.onFinish();
			listView.onRefreshComplete();
		}

	}
	
	class MyAdapter extends BaseAdapter{
		private LayoutInflater inflater;
		private DisplayImageOptions mUnknowDisplayImageOptions;
		public List<DoctorPatientListEntity> entities=new ArrayList<DoctorPatientListEntity>();;
		private ImageLoader instance;
		MyAdapter(){
			inflater=getLayoutInflater().from(MyDoctorMoreActivity.this);
			instance = ImageLoader.getInstance();
			mUnknowDisplayImageOptions = createUnknowHeaderDisplayImageOptions(MyDoctorMoreActivity.this);
		}
		
		DisplayImageOptions createUnknowHeaderDisplayImageOptions(Context context){
			DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder(context)
			.showStubImage(R.drawable.default_jiugong_icon)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.cacheOnDisc(new File(StorageUtils.getHeadersPath()))
			.cacheInMemory()
			.displayer(new RoundedBitmapDisplayer(5))
			.build();
			return displayImageOptions;
		}
		@Override
		public int getCount() {
			if(entities.size()==0) return 0;
			return entities.size()-3;
		}

		@Override
		public Object getItem(int position) {
			return entities.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			DoctorPatientListEntity entity= entities.get(position+3);
			ViewHolder holder;
			if(convertView==null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.nine_image_list_item, null);
				holder.mHeader = (GridView) convertView.findViewById(R.id.head_image);
				holder.mName = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.mName.setText(entity.getName()+"("+entity.getNum()+")");
			holder.mHeader.setAdapter(new myGrived(entity.getUrlEntities()));
//			instance.displayImage(FriendUtil.getHeaderPath(entity.getmImageEntity().getList()), holder.mHeader, mUnknowDisplayImageOptions);
			return convertView;
		}
		class ViewHolder {
			GridView mHeader;
			TextView mName;
		}
		
	}
	class myGrived extends BaseAdapter{
		private  List<UrlEntity> urlEntities=new ArrayList<UrlEntity>();
		private ImageLoader instance;
		public myGrived(List<UrlEntity> urlEntities) {
			instance = ImageLoader.getInstance();
			this.urlEntities.clear();
			if(urlEntities!=null)
			this.urlEntities.addAll(urlEntities);
		}

		@Override
		public int getCount() {
			return urlEntities.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView=new ImageView(getApplicationContext());
			imageView.setLayoutParams(new AbsListView.LayoutParams(33,33));
			instance.displayImage(urlEntities.get(position).getSex(), urlEntities.get(position).getUrl(), imageView);
			return imageView;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DoctorPatientListEntity item = (DoctorPatientListEntity) myAdapter.getItem(position+2);
		Intent intent = new Intent(this, MyDoctorDetailListActivity.class);
		intent.putExtra("name", item.getName());
		intent.putExtra("index",item.getId());
		startActivity(intent);
		
		
//		intent = new Intent(getActivity(), MyDoctorDetailListActivity.class);
//		intent.putExtra("name", mNineImageAdapter.entities.get(childPosition+1).getName());
//		intent.putExtra("index", mNineImageAdapter.entities.get(childPosition+1).getId());
//		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
		
	}
}
